<?php
require_once('AbstractController.php');
include_once ("arc/ARC2.php");
/**
 * PublicationController.php
 *
 * @author		b.wenneker <b.wenneker@gmail.com>, 2010
 */
class PublicationController extends Thesis_AbstractController
{

	public function overviewAction ()
	{
		$store = $this->_getStore();
		$query =
			'PREFIX dc: <http://purl.org/dc/elements/1.1/>
  				SELECT ?title FROM <http://wisserver.st.ewi.tudelft.nl> WHERE {
				?d dc:title ?title .
   			}';
				
		$rows = $store->query($query, 'rows');
		$titleCounts = array_fill_keys(range('a', 'z'), 0);
		foreach ((array) $rows as $row) {
			$name = lcfirst(ltrim($row['title']));
			if(!isset($titleCounts[$name[0]])){
				$titleCounts[$name[0]] = 0;
			}
			$titleCounts[$name[0]] += 1;
		}
		
		$this->view->assign('titleCounts', $titleCounts);
	}
	
	public function publicationsAction ()
	{
		
		$character = $this->getRequest()->getParam('queryparam');
		$store = $this->_getStore();
		
		$query =
			'PREFIX dc: <http://purl.org/dc/elements/1.1/>
  				SELECT ?title FROM <http://wisserver.st.ewi.tudelft.nl> WHERE {
				?d dc:title ?title .
				FILTER regex(?title, "^' . $character . '", "i")
   			} ORDER BY ?title';
		
		$rows = $store->query($query, 'rows');
		$titles = array();
		
		foreach ($rows as $row) {
			$titles[] = $row['title'];
		}
		
		$this->view->assign('character', $character);
		$this->view->assign('titles', $titles);
	}
	
	public function indexAction(){
		
		$title = $this->getRequest()->getParam('queryparam', 'no_title');
		$store = $this->_getStore();
		
		if($this->_contentType == 'page'){
			// User agent requests a html page, let's assemble one about this
			// very publication.
			$query =
				'PREFIX foaf: <http://xmlns.com/foaf/0.1/>
				 PREFIX dc: <http://purl.org/dc/elements/1.1/>
				 PREFIX dcterms: <http://purl.org/dc/terms/>
				 PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
				 PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
				 PREFIX swrc: <http://swrc.ontoware.org/ontology#>
				  
				 SELECT * FROM <http://wisserver.st.ewi.tudelft.nl> WHERE {
					?a dc:title "'. $title .'" .
			        ?a ?b ?c
					FILTER(!isblank(?c))
				  }';
			
			$rows = $store->query($query, 'rows');
			$data = array();
			
			foreach ($rows as $row) {
				$data[$row['c']] = $row['b'];
			}			
			
			$this->view->assign('title', $title);
			$this->view->assign('data', $data);

		}else if($this->_contentType == 'data'){
			// User agent requests rdf data.
			$store = $this->_getStore();
			
			$config = array('remote_store_endpoint' => 'http://wisserver.st.ewi.tudelft.nl:8893/sparql');
			$ssp = ARC2::getSPARQLScriptProcessor($config);
			
			$query = 'PREFIX dc: <http://purl.org/dc/elements/1.1/>
			  select ?uri where {
				?uri dc:title "'. $title .'" }';
			
			if ($rows = $store->query($query, 'rows')) {
				foreach ($rows as $row) {
					$uri = $row['uri'];
				}
			}
			
			$scr = '
			  PREFIX dcterms: <http://purl.org/dc/terms/>
			  PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
			  PREFIX dc: <http://purl.org/dc/elements/1.1/>
			  PREFIX swrc: <http://swrc.ontoware.org/ontology#>
			  PREFIX foaf: <http://xmlns.com/foaf/0.1/>
			  PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
			  
			  DESCRIBE <'.$uri.'>
			';
			
			$scr = urlencode($scr);
			$contents = file_get_contents('http://wisserver.st.ewi.tudelft.nl:8893/sparql?default-graph-uri=&query='.$scr.'&format=application/rdf+xml');
			
			header('Content-Type: application/rdf+xml; charset=utf-8');
			header('Content-Disposition: attachment; filename="'. $title .'.rdf"');
			echo $contents;
			
			exit();
		}
	}
	
	public function dbpediaAction(){
	
		$title = $this->getRequest()->getParam('queryparam', 'no_title');
		
		$store = $this->_getStore();
		$query = '
		  PREFIX owl: <http://www.w3.org/2002/07/owl#>

		  SELECT ?geonames_uri WHERE {
				<http://wisserver.st.ewi.tudelft.nl/resource/publication/'.urlencode($title).'> <http://dbpedia.org/ontology/Place> ?b .
				?b owl:sameAs ?geonames_uri
		  }';
		 
		if ($rows = $store->query($query, 'rows')) {			
			foreach ($rows as $row) {
				$geonames = $row['geonames_uri'];
			}
		}
		else{
			print_r($store->getErrors());
		}
		
		$config = array('remote_store_endpoint' => 'http://ldsr.ontotext.com/sparql');
		$store = ARC2::getRemoteStore($config);
		$q = '
		  PREFIX geo-pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>

		  select ?a ?b where {<'.$geonames.'> geo-pos:lat ?a .
				<'.$geonames.'> geo-pos:long ?b} LIMIT 1
			 ';

		if ($rows = $store->query($q, 'rows')) {
			foreach ($rows as $row) {
				$location['x'] = $row['a'];
				$location['y'] = $row['b'];
			}
		}		
			
		
		//if the paper has a location, get it extra information from DBPedia
		if($geonames != ''){
			$config = array('remote_store_endpoint' => 'http://ldsr.ontotext.com/sparql');
			$store = ARC2::getRemoteStore($config);


			$q = '
				PREFIX foaf: <http://xmlns.com/foaf/0.1/>
				PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
				PREFIX dbp-prop: <http://dbpedia.org/property/>
				PREFIX geo-ont: <http://www.geonames.org/ontology#>

				SELECT ?code ?depic ?comment ?title ?caption ?link where {
					<'.$geonames.'> 	<http://www.geonames.org/ontology#inCountry> ?code .
						OPTIONAL {	<'.$geonames.'>	foaf:depiction ?depic } .
						OPTIONAL {	<'.$geonames.'>	rdfs:comment ?comment . FILTER ( lang(?comment) = "en") } .
						OPTIONAL {	<'.$geonames.'>	geo-ont:name ?title } .
						OPTIONAL {	<'.$geonames.'>	dbp-prop:imageCaption ?caption . FILTER (lang(?caption) = "en" ) } .
						OPTIONAL {	<'.$geonames.'>	dbp-prop:web ?link . FILTER isIRI(?link) } .
					}
					';

			if ($rows = $store->query($q, 'rows')) {
				foreach ($rows as $row) {
					$country = explode('#',$row['code']);
					if(!IsSet($codes[$country[1]])){
						$codes[$country[1]] = 1;
					}
					else{
						$codes[$country[1]] += 1;
					}
					$dbpedia['depic'] = $row['depic'];
					$dbpedia['comment'] = $row['comment'];
					$dbpedia['title'] = $row['title'];
					$dbpedia['caption'] = $row['caption'];
					$dbpedia['link'] = $row['link'];
				}
			}
		}
		
		$this->view->assign('title', $title);
		$this->view->assign('location', $location);
		$this->view->assign('dbpedia', $dbpedia);
	
	}
	
	public function mapAction(){
	
		$title = $this->getRequest()->getParam('queryparam', 'no_title');
		
		$store = $this->_getStore();
		$query = '
		  PREFIX owl: <http://www.w3.org/2002/07/owl#>

		  SELECT ?geonames_uri WHERE {
				<http://wisserver.st.ewi.tudelft.nl/resource/publication/'.urlencode($title).'> <http://dbpedia.org/ontology/Place> ?b .
				?b owl:sameAs ?geonames_uri
		  }';
		 
		if ($rows = $store->query($query, 'rows')) {			
			foreach ($rows as $row) {
				$geonames = $row['geonames_uri'];
			}
		}
		else{
			print_r($store->getErrors());
		}
		
		$config = array('remote_store_endpoint' => 'http://ldsr.ontotext.com/sparql');
		$store = ARC2::getRemoteStore($config);
		$q = '
		  PREFIX geo-pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>

		  select ?a ?b where {<'.$geonames.'> geo-pos:lat ?a .
				<'.$geonames.'> geo-pos:long ?b} LIMIT 1
			 ';

		if ($rows = $store->query($q, 'rows')) {
			foreach ($rows as $row) {
				$location['x'] = $row['a'];
				$location['y'] = $row['b'];
			}
		}
				
		$this->view->assign('title', $title);
		$this->view->assign('location', $location);
	
	}
}